﻿package 
{
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.resource.core.*;
    import extend.language.*;
    import extend.php.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;
    import flash.net.*;
    import flash.text.*;
    import mortal.common.global.*;
    import mortal.common.preLoadPage.*;
    import mortal.common.swfPlayer.*;

    public class Preloader extends Sprite
    {
        private var _textField:TextField;
        private var _isLoadingShow:Boolean = false;

        public function Preloader()
        {
            addEventListener("allComplete", this.allCompleteFun, false, int.MAX_VALUE, false);
            this.entrance();
            return;
        }// end function

        private function allCompleteFun(event:Event) : void
        {
            event.stopImmediatePropagation();
            event.stopPropagation();
            event.preventDefault();
            return;
        }// end function

        private function entrance() : void
        {
            if (stage)
            {
                this.init();
            }
            else
            {
                this.addEventListener(Event.ADDED_TO_STAGE, this.init);
            }
            return;
        }// end function

        private function init(event:Event = null) : void
        {
            Global.instance.initStage(stage);
            stage.quality = StageQuality.LOW;
            GameLoadBar.instance.gameRoot = stage;
            ParamsConst.instance.init(stage);
            PreLanguage.init(this.initGame);
            return;
        }// end function

        private function initGame() : void
        {
            if (PHPSender.isCreateRole)
            {
                PHPSender.clearUserName(ParamsConst.instance.username);
                PHPSender.sendToURLByPHP(SenderType.Preloader);
            }
            this.onForgameLogoPlayComplete();
            return;
        }// end function

        private function onForgameLogoPlayComplete() : void
        {
            PreLoginProxy.instance.rootStage = stage;
            this.onloadPrePage();
            return;
        }// end function

        private function loginSuccessHandler(param1 = null) : void
        {
            PreLoginProxy.instance.removeEventListener(PreLoginProxy.LOGIN_SUCCESS, this.loginSuccessHandler);
            PreLoginProxy.instance.removeEventListener(PreLoginProxy.CreateRoleSuccess, this.createRoleSucessHandler);
            return;
        }// end function

        private function createRoleSucessHandler(param1 = null) : void
        {
            this.loginSuccessHandler(null);
            return;
        }// end function

        private function onloadPrePage() : void
        {
            GameLoadBar.resourceLoader = new ClientLoader(true);
            GameLoadBar.resourceLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.loadCompleteHandler);
            GameLoadBar.resourceLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.loadProgressHandler);
            GameLoadBar.resourceLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.loadErrorHandler);
            GameLoadBar.resourceLoader.load(new URLRequest(PathConst.mainPath + "assets/modules/PreloaderPage/preLoadPage.swf" + "?v=" + ParamsConst.instance.flashVersion));
            this.addLoadingShowJsCall();
            return;
        }// end function

        private function addLoadingShowJsCall() : void
        {
            try
            {
                if (ExternalInterface.available)
                {
                    ExternalInterface.addCallback("setLoadingShow", this.setLoadingShow);
                }
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        private function setLoadingShow() : void
        {
            if (!this._isLoadingShow)
            {
                this._isLoadingShow = true;
                if (!GameLoadBar.instance.isAdded)
                {
                    GameLoadBar.instance.show();
                }
            }
            return;
        }// end function

        private function loadCompleteHandler(event:Event) : void
        {
            GameLoadBar.resourceLoader.removeEventListener(Event.COMPLETE, this.loadCompleteHandler);
            GameLoadBar.instance.addEventListener(Event.COMPLETE, this.onComplete);
            GameLoadBar.instance.initLoaderBar();
            GameLoadBar.instance.setProgress(PreloaderConfig.LOGIN_GAME, 0.8, 1);
            this.login();
            return;
        }// end function

        private function login() : void
        {
            PreLoginProxy.instance.login();
            PreLoginProxy.instance.addEventListener(PreLoginProxy.LOGIN_SUCCESS, this.loginSuccessHandler);
            PreLoginProxy.instance.addEventListener(PreLoginProxy.CreateRoleSuccess, this.createRoleSucessHandler);
            return;
        }// end function

        private function onComplete(event:Event) : void
        {
            return;
        }// end function

        private function showIntroduceMovie() : void
        {
            PreLoginProxy.instance.isIntroduceMovieEnd = false;
            var _loc_1:* = new FLVPlayer();
            _loc_1.play(PathConst.mainPath + "assets/modules/PreloaderPage/gameIntroduceMovie.flv", this.moviePlayEnd);
            _loc_1.setSize(stage.stageWidth, stage.stageHeight);
            stage.addChild(_loc_1);
            return;
        }// end function

        private function moviePlayEnd(param1:FLVPlayer) : void
        {
            stage.removeChild(param1);
            param1.dispose(false);
            PreLoginProxy.instance.isIntroduceMovieEnd = true;
            return;
        }// end function

        private function loadProgressHandler(event:ProgressEvent) : void
        {
            return;
        }// end function

        private function loadErrorHandler(event:ErrorEvent) : void
        {
            Log.system("资源加载失败！" + (event.target as LoaderInfo).url);
            return;
        }// end function

    }
}
